package com.jflm.util;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ******************  类说明  *********************
 * class       :  AESUtil
 * @author     :  hejinyun@umpay.com
 * @version    :  1.0  
 * description :  AES加密、解密工具类
 * @see        :                        
 * ***********************************************
 */
public class AESUtil {
	
	private static final Logger LOG = LoggerFactory.getLogger(AESUtil.class);
	private static final String TRANSFORMATION = "AES/CBC/PKCS5Padding";
	private static final String ALGORITHM = "AES";
	private static final byte[] BYTE_IV_PARAMETER = {'0', '1', '2', '3', '4', 
		'5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
	
	/**
	 * ********************************************
	 * method name   : encrypt 
	 * description   : 加密
	 * @return       : String
	 * @param        : @param source
	 * @param        : @return
	 * modified      : hejinyun@umpay.com ,  2017-4-29  下午10:09:01
	 * @see          : 
	 * *******************************************
	 */
	public static String encrypt(String source, String aesKey, String charset){
		String result = "";
		try {
			Cipher cipher = Cipher.getInstance(TRANSFORMATION);
			byte[] raw = aesKey.getBytes();
			SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);
			// 使用CBC模式，需要一个向量iv，可增加加密算的强度
			IvParameterSpec iv = new IvParameterSpec(BYTE_IV_PARAMETER);
			cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, iv);
			byte[] encrypted = cipher.doFinal(source.getBytes(charset));
			result = Base64Utils.encode(encrypted);
		} catch (Exception e){
			LOG.error("#encrypt() AES加密异常", e);
		}
		return result;
	}
	
	/**
	 * ********************************************
	 * method name   : decrypt 
	 * description   : 解密
	 * @return       : String
	 * @param        : @param ciphertext
	 * @param        : @return
	 * modified      : hejinyun@umpay.com ,  2017-4-29  下午10:10:16
	 * @see          : 
	 * *******************************************
	 */
	public static String decrypt(String ciphertext, String aesKey, String charset){
		try {
			byte[] raw = aesKey.getBytes();
			SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);
			Cipher cipher = Cipher.getInstance(TRANSFORMATION);
			IvParameterSpec iv = new IvParameterSpec(BYTE_IV_PARAMETER);
			cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, iv);
			byte[] encrypted = Base64Utils.decode(ciphertext);
			byte[] decrypt = cipher.doFinal(encrypted);
			return new String(decrypt, charset);
		} catch (Exception e){
			LOG.error("#decrypt() AES解密异常", e);
		}
		return null;
	}
	
	public static void main(String[] args) throws Exception {
		String aesKey = "abcdef0123456789";
		String source = "hejinyun@umpay.com";
		String result = AESUtil.encrypt(source, aesKey, "utf-8");
		System.out.println(result);
		
		String orgSource = AESUtil.decrypt(result, aesKey, "utf-8");
		System.out.println(orgSource);
	}
	
}
